package com.kanshu.basictest.ui.activity.thread;

import android.view.View;
import android.widget.AdapterView;

import com.kanshu.basictest.R;
import com.kanshu.basictest.model.menu.MenuItem;
import com.kanshu.basictest.service.thread.DownloadFileTask;
import com.kanshu.basictest.ui.base.BaseListMenuActivity;

import java.util.ArrayList;
import java.util.List;

public class AsyncTaskTestActivity extends BaseListMenuActivity {


    private DownloadFileTask mTask = new DownloadFileTask();

    @Override
    protected String provideBarTitle() {
        return "AsyncTask测试";
    }

    public static final String TAG = "AsyncTaskTestActivity";

    @Override
    protected List<? extends MenuItem> provideMenuData() {
        List<MenuItem> menuItems = new ArrayList<>();
        menuItems.add(0, new MenuItem("启动Task", R.color.blue_200));//实例应用
        menuItems.add(1, new MenuItem("终止Task", R.color.blue_200));//实例应用
        return menuItems;
    }

    @Override
    protected void init() {
        super.init();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        switch (position) {
            case 0://基础
                startTask();
                break;
            case 1://基础
                stopTask();
                break;
            default:
                break;

        }
    }


    private void startTask() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                DownloadFileTask task = new DownloadFileTask();
                task.execute();
            }
        }.start();
    }

    private void stopTask() {
        mTask.cancel(true);
    }

}
